package kotlin


class Point(x: Int, y: Int) {

    var x: Int = 0

    var y: Int = 0

    init {
        this.x = x
        this.y = y
    }

    override fun toString(): String {
        return "(x=$x, y=$y)"
    }

    // 必须要有 operator 修饰
    // c = a + b
    operator fun plus(p: Point): Point {
        return Point(x + p.x, y + p.y)
    }

    // c = a - b
    operator fun minus(p: Point): Point {
        return Point(x - p.x, y - p.y)
    }

    // b = a++
    operator fun inc(): Point {
        return Point(x + 1, y + 1)
    }

    // b = a--
    operator fun dec(): Point {
        return Point(x - 1, y - 1)
    }

    // a += 3  赋值操作符重载可以是别的类型
    operator fun plusAssign(n: Int) {
        x += n
        y += n
    }

    // a > b, a >= b, a < b , a <= b
    operator fun compareTo(p: Point): Int {
        var r = x.compareTo(p.x)
        if (r != 0) {
            return r
        }
        return y.compareTo(p.y)
    }

}

fun runPlusAssign() {
    var a = Point(1, 1)
    a += 3
    println("a = ${a}")

    var b = Point(1, 1)
    b += a
    println("b = ${b}")
}

fun runCompareTo() {
    var a = Point(1, 1)
    var b = Point(1, 2)
    println("a = $a, b = $b")
    println("a > b : ${a > b}")
    println("a < b : ${a < b}")
}